﻿using System;
using System.Diagnostics;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using LiveHub.AvaloniaApp.Views;
using LiveHub.DataModels;

namespace LiveHub.AvaloniaApp.Pages;

public partial class LoginView : UserControl
{
    public LoginView()
    {
        InitializeComponent();
    }

    private async void LoginClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(NameBox.Text) || string.IsNullOrEmpty(IdBox.Text))
        {
            return;
        }

        var view = ViewOpera.GetView<MainWindow>(this);
        if (view == null) return;
        if (await view.Login(new LoginModel() { Name = NameBox.Text, Id = IdBox.Text }))
        {
            view.NotificationShow("LiveHub", "登录成功");
        }
        else
        {
            view.NotificationShow("LiveHub", "登录失败", NotificationType.Error);
        }
    }

    private void SignupClick(object? sender, RoutedEventArgs e)
    {
        var uri = new Uri("https://www.xauat.site/Signup");
        Process.Start(new ProcessStartInfo(uri.ToString())
            { UseShellExecute = true, Verb = "open" });
    }
}